/*
 * [y] hybris Platform
 *
 * Copyright (c) 2000-2017 SAP SE
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of SAP
 * Hybris ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the
 * terms of the license agreement you entered into with SAP Hybris.
 */
package acc.asset.sync.creator;

import de.hybris.deltadetection.ItemChangeDTO;
import de.hybris.deltadetection.enums.ChangeType;
import de.hybris.y2ysync.task.runner.internal.ExportScriptCreator;

import java.util.Collection;
import java.util.Objects;


/**
 *
 */
public class DemoExportScriptCreator extends ExportScriptCreator
{
	private final String impExHeader;
	private final String typeCode;
	private final Collection<ItemChangeDTO> changes;

	public DemoExportScriptCreator(final String impExHeader, final String typeCode, final Collection<ItemChangeDTO> changes)
	{
		super(impExHeader, typeCode, changes);

		Objects.requireNonNull(impExHeader, "impExHeader can't be null");
		Objects.requireNonNull(typeCode, "typeCode can't be null");
		Objects.requireNonNull(changes, "changes can't be null");
		this.impExHeader = impExHeader;
		this.typeCode = typeCode;
		this.changes = changes;
	}

	@Override
	public String createInsertUpdateExportScript()
	{
		final StringBuilder insertUpdatePart = new StringBuilder("\"#% impex.setTargetFile( \"\"")
				.append(this.getFileNameForInsertedOrUpdatedItems()).append("\"\")\"\n").append(this.getInsertUpdateHeader());

		boolean anyItemToInsertUpdate = false;
		if (this.changes != null && !this.changes.isEmpty())
		{
			insertUpdatePart.append("\n\"#% impex.exportItems(new String[] {\n");
			for (final ItemChangeDTO change : this.changes)
			{
				if (ChangeType.DELETED == change.getChangeType())
				{
					continue;
				}
				anyItemToInsertUpdate = true;
				insertUpdatePart.append("\"\"").append(change.getItemPK()).append("\"\",");
			}
			insertUpdatePart.append("\n})\"\n");
		}
		else
		{
			//TODO how to deal records deleted wei.f.zhang
			anyItemToInsertUpdate = true;
			insertUpdatePart.append("\n\"#% impex.exportItems(\"\"" + this.typeCode + "\"\" , false)\"\n");
		}

		final StringBuilder result = new StringBuilder();
		if (anyItemToInsertUpdate)
		{
			result.append(insertUpdatePart);
		}

		return result.toString();

	}

	@Override
	public String createDeleteCsv()
	{
		if (this.changes == null || this.changes.isEmpty())
		{
			return "";
		}
		else
		{
			final StringBuilder result = new StringBuilder();
			this.changes.stream().filter(change -> ChangeType.DELETED == change.getChangeType()).forEach(change -> {
				result.append(change.getInfo()).append("\n");
			});
			return result.toString();
		}

	}

	public String getTypeCode()
	{
		return this.typeCode;
	}

	public String getFileNameForRemovedItems()
	{
		return String.valueOf(this.getTypeCode()) + "_remove.csv";
	}

	public String getFileNameForInsertedOrUpdatedItems()
	{
		return String.valueOf(this.getTypeCode()) + "_insert_update.csv";
	}

	public String getRemoveHeader()
	{
		return "REMOVE " + this.getTypeCode() + ";" + this.impExHeader;
	}

	public String getInsertUpdateHeader()
	{
		return "INSERT_UPDATE " + this.getTypeCode() + ";" + this.impExHeader;
	}

}
